//
//  Measure+Measure.m
//  ClinicalCollections
//
//  Created by janakiraman gopinath on 2/10/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "Measure+Measure.h"

@implementation Measure (Measure)

+ (void) measure:(NSDictionary *) measures
        withType:(NSString *) type
 usingGroupTypes:(GroupTypes *) groupTypes
usingClinicalTrialGov:(ClinicalTrialGov *) clinicalTrialGov
    usingContext:(NSManagedObjectContext *) context
{
    
    Measure  *measure;
    measure = (Measure *)[NSEntityDescription insertNewObjectForEntityForName:@"Measure"
                                                       inManagedObjectContext:context];
    
    if (![[measures valueForKey:@"param"] isEqual:[NSNull null]])
        measure.param = [measures valueForKey:@"param"];
    if (![[measures valueForKey:@"title"] isEqual:[NSNull null]])
        measure.title = [measures valueForKey:@"title"];
    if (![[measures valueForKey:@"units"] isEqual:[NSNull null]])
        measure.unit = [measures valueForKey:@"units"];
    if (![[measures valueForKey:@"dispersion"] isEqual:[NSNull null]])
        measure.dispersion = [measures valueForKey:@"dispersion"];
    if (![[measures valueForKey:@"description"] isEqual:[NSNull null]])
        measure.desc = [measures valueForKey:@"description"];
    
    
    measure.groupTypes = groupTypes;
    //NSLog(@"Mesure Inserted %@" , measure);
    
    NSMutableArray *categories;
    NSMutableArray *categoriesMutable = [[NSMutableArray alloc] initWithCapacity:MAX_CAPACITY];
    
    /*if ([type isEqual:@"outcome_list"]) {
        NSLog(@"%@------%@" , [measures valueForKeyPath:@"category_list.category"] , [measures valueForKeyPath:@"category_list"]);
    }*/
    if ([type isEqual:@"baseline"]|| [type isEqual:@"outcome_list"])
        categories = [measures valueForKeyPath:@"category_list.category"];
    else if([type isEqual:@"participant_flow"]) {
        categories = [measures valueForKeyPath:@"milestone_list.milestone"];
        
        [categoriesMutable  addObjectsFromArray:[[CommonAPI class] addkey:@"super_title" keyValue:@"TotalCounts" toDictionaryObjectArray:categories]];
            
        categories = [measures valueForKeyPath:@"drop_withdraw_reason_list.drop_withdraw_reason"];
        
       [categoriesMutable  addObjectsFromArray:[[CommonAPI class] addkey:@"super_title" keyValue:@"WithDrawCounts" toDictionaryObjectArray:categories]];
        
        categories = categoriesMutable;
    }
    
    for (int i = 0; i < categories.count; i++)
        [[Categories class] categories:categories[i]
                              withType:type
                          usingMeasure:measure
                 usingClinicalTrialGov:clinicalTrialGov
                          usingContext:context];
    
    return;
}

@end
